<?php
class My_Authuser{
    private $_request;
    private $_authUser;
    public function __construct(){
        $this->_request = Zend_Controller_Front::getInstance()->getRequest();
        $auth = Zend_Auth::getInstance();
        $auth->setStorage(new Zend_Auth_Storage_Session(SESSION_NAMESPACE.'_default'));
        $this->_auth = $auth;
        
        $this->_helperRedirector = new Zend_Controller_Action_Helper_Redirector();
    }
    
    public function hasIdentity(){
        if(!$this->_auth->hasIdentity()){
            return false;
        }
        $authInfo = $this->_auth->getIdentity();
        $modelUser = new Default_Model_User();
        $userCheck = $modelUser->getDataById($authInfo['userId']);
        if($userCheck['sessionId'] != session_id()){
            $this->_auth->clearIdentity();
            return false;
        }
        
        return $this->_auth;
    }
    public static function logout($sessNamespace = null){
        if(!$sessNamespace){
            $sessNamespace = SESSION_NAMESPACE.'_default';
        }
        $auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session($sessNamespace));
        if($auth->hasIdentity()){
            $auth->clearIdentity();
            return true;
        }
        return false;
    }    
    public function checkAuthentication($request = null){
        $authInfo       = $this->_auth->getIdentity();       
        return $authInfo;
    }   
}